package com.qf.controller;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qf.entity.User;
import com.qf.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/addUser")
    public Boolean addUser(@RequestBody User user) {
        return userService.insert(user);
    }

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userService.selectById(id);
    }

    @RequestMapping("/updateUser")
    public Boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    // @RequestBody:把JSON字符串转成一个对象
    // url:xxxxxx/user/getUserPage
    // context-type:applicatin/json
    // data:{"username":"admin","age":123}
    @RequestMapping("/getUserPage")
    public Page<User> getUserPage(@RequestBody Map<String, Object> map) {

        Object username = map.get("username");
        Object email = map.get("email");
        Object phone = map.get("phone");
        Object current = map.get("current");
        Object size = map.get("size");

        // 条件查询
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            entityWrapper.like("username", username.toString(), SqlLike.DEFAULT); // username like '%张三%'
        }
        if (!StringUtils.isEmpty(email)) {
            entityWrapper.eq("email", email.toString());
        }
        if (!StringUtils.isEmpty(phone)) {
            entityWrapper.eq("phone", phone);
        }

        Page<User> page = new Page<>();
        if (!StringUtils.isEmpty(current)) {
            page.setCurrent(Integer.parseInt(current.toString()));
        }

        if (!StringUtils.isEmpty(size)) {
            page.setSize(Integer.parseInt(size.toString()));
        }
        return userService.selectPage(page, entityWrapper);
    }


    // @PathVariable:获取的地址栏上面的值
    // url:xxx/user/deleteUserById/100

    // @RequestParam:取得时问号后面的值
    // url:xxxxx/user/addUser?usernam=admin&age=123
    // contex-type:form-data
    @RequestMapping("/deleteUserById/{id}")
    public Boolean deleteUserById(@PathVariable Integer id) {
        return userService.deleteById(id);
    }

    @RequestMapping("/batchDelUser")
    public void batchDelUser(@RequestBody List<Integer> userIds) {
        log.debug(Arrays.toString(userIds.toArray()));
        userService.deleteBatchIds(userIds);
    }

    @RequestMapping("/findUser")
    public User findUser(@RequestParam("field") String field, @RequestParam("value") String value) {
        EntityWrapper<User> entityWrapper = new EntityWrapper();
        entityWrapper.eq(field, value);
        return userService.selectOne(entityWrapper);
    }

    @RequestMapping(
            value = "/test", // and
            method = RequestMethod.GET,
            params = "age=10",
            headers = "context-type:applicatin/json",

            // 服务端生产数据的类型是text/html;charset=utf-8
            produces = "text/html;charset=utf-8", // 解决服务端响应的乱码的问题
            consumes = "application/json" // 消费的数据是JSON
    )
    public void test(HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");

    }
}
